package de.tuberlin.dima.minidb.parser;


import java.util.ArrayList;
import java.util.Iterator;


/**
 * A parse tree node that represents a referenced table.
 * 
 * @author Stephan Ewen (sewen@cs.tu-berlin.de)
 */
public class TableReference implements ParseTreeNode
{
	/**
	 * The name of the table referenced by this node.
	 */
	protected final String tableName;
	
	/**
	 * The alias name of the table 
	 */
	protected final String aliasName;
	
	
	/**
	 * Create a reference node for the table with the given name.
	 * 
	 * @param tabName The name of the table.
	 */
	public TableReference(String tabName, String alias)
	{
		this.tableName = tabName;
		this.aliasName = alias;
	}
	
	
	/**
	 * Gets the name of the table that is referenced.
	 * 
	 * @return The table name.
	 */
	public String getTableName()
	{
		return tableName;
	}
	
	/**
	 * Gets the alias name of the table that is referenced.
	 * 
	 * @return The table's alias name.
	 */
	public String getAliasName()
	{
		return aliasName;
	}

	
	/* (non-Javadoc)
	 * @see de.tuberlin.dima.minidb.parser.ParseTreeNode#getChildren()
	 */
	public Iterator<ParseTreeNode> getChildren()
	{
		return new ArrayList<ParseTreeNode>(0).iterator();
	}

	/* (non-Javadoc)
	 * @see de.tuberlin.dima.minidb.parser.ParseTreeNode#getNodeContents()
	 */
	public String getNodeContents()
	{
		return tableName + ' ' + aliasName;
	}

	/* (non-Javadoc)
	 * @see de.tuberlin.dima.minidb.parser.ParseTreeNode#getNodeName()
	 */
	public String getNodeName()
	{
		return "Table Reference";
	}
	
	/* (non-Javadoc)
	 * @see de.tuberlin.dima.minidb.parser.ParseTreeNode#getNumberOfChildren()
	 */
	public int getNumberOfChildren()
	{
		return 0;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		return getNodeContents();
	}


	/* (non-Javadoc)
	 * @see de.tuberlin.dima.minidb.parser.ParseTreeNode#isIdenticalTo(de.tuberlin.dima.minidb.parser.ParseTreeNode)
	 */
	public boolean isIdenticalTo(ParseTreeNode node)
	{
		if (node != null && node instanceof TableReference)
		{
			TableReference other = (TableReference) node;
			
			return ( (tableName == null && other.tableName == null) ||
					 (tableName != null && other.tableName != null &&
					  tableName.equalsIgnoreCase(other.tableName))
				   ) && (
					 (aliasName == null && other.aliasName == null) ||
					 (aliasName != null && other.aliasName != null &&
					  aliasName.equalsIgnoreCase(other.aliasName))
				   );
		}
		
		return false;
		
	}

}
